/**
 * 
 */
package com.sl.init;

import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.sl.entity.User;
import com.sl.service.GithubLookupService;

/**
 * @author lunshi
 *
 */
@Component
@Order(1)  //存在多个CommandLineRunner的时候执行顺序
public class AppRunner implements CommandLineRunner {
	private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);

	@Autowired
    GithubLookupService githubLookupService;
    
	public void run(String... args) throws Exception {
		long start = System.currentTimeMillis();

        // 3个用户同时请求
        Future<User> page1 = githubLookupService.findUser("PivotalSoftware");
        Future<User> page2 = githubLookupService.findUser("CloudFoundry");
        Future<User> page3 = githubLookupService.findUser("Spring-Projects");
       
        //等待三个请求都完成
        while(!(page1.isDone()&&page2.isDone()&&page3.isDone())) {
        	Thread.sleep(20);
        }
        //打印请求结果和消耗时间
        logger.info("Elapsed time: " + (System.currentTimeMillis() - start));
        logger.info("--> " + page1.get());
        logger.info("--> " + page2.get());
        logger.info("--> " + page3.get());
	}

}
